﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using HHG.Common;

namespace OnLineExam.Web.mag.Handler
{
    /// <summary>
    /// FillBlankHandler 的摘要说明
    /// </summary>
    public class FillBlankHandler : IHttpHandler
    {
        private OnLineExam.Data.EfRepository<OnLineExam.Data.FillBlankProblem> _efRepository;

        public void ProcessRequest(HttpContext context)
        {
            _efRepository = new Data.EfRepository<Data.FillBlankProblem>(new OnLineExam.Data.OnlineExamEntities());
            string type = context.Request.Params["type"];

            var ID = context.Request.QueryString["ID"];

            OnLineExam.BLL.FillBlankProblem bll = new OnLineExam.BLL.FillBlankProblem();

            switch (type)
            {
                case "getlist":
                    #region 获取列表
                    int page = int.Parse(context.Request.Params["page"]);
                    int rows = int.Parse(context.Request.Params["rows"]);
                    string depid = context.Request.Params["depid"];
                    int startIndex = (page - 1) * rows + 1;
                    int endIndex = (page) * rows;

                    var paperId = context.Request.Params["paperId"].GetInt();
                    var query = _efRepository.TableNoTracking;
                    if (paperId > 0)
                        query = query.Where(p => p.PaperID == paperId);

                    int RecordCount = query.Count();
                    var items = query.OrderBy(s => s.ID).Skip((page - 1) * rows).Take(rows);


                    context.Response.Write(new DataGrid(RecordCount, items.ToList()).SerializeObject());
                    #endregion
                    break;
                case "edit":
                    #region 根据ID获取信息

                    if (!string.IsNullOrEmpty(ID))
                    {
                        var m = bll.GetModel(int.Parse(ID));
                        m.ExamTime = DateTime.Now;

                        var rsult = HHG.Common.Json.GetJson<OnLineExam.Model.FillBlankProblem>(m);
                        context.Response.Write(rsult);
                    }

                    #endregion
                    break;
                case "addsave":
                    #region 提交修改
                    depid = context.Request.Params["selectDep"];
                    //ID = context.Request.Params["ID"];
                    //var o_model = bll.GetModel(int.Parse(ID));
                    var new_model = new OnLineExam.Model.FillBlankProblem();
                    //new_model.ID = int.Parse(ID);
                    new_model.DepID = int.Parse(depid);
                    new_model.CourseID = 0;//int.Parse(context.Request.Params["selectCourse"]);
                    new_model.FrontTitle = context.Request.Params["txtTitle"];
                    new_model.BackTitle = "0";
                    new_model.PaperID =int.Parse( context.Request.Params["selectPaper"]);
                    new_model.Answer = context.Request.Params["txtAnswer"];

                    int nID = bll.Add(new_model);
                    context.Response.Write(nID);//修改成功，返回ID,失败，返回0

                    #endregion
                    break;
                case "editsave":
                    #region 提交修改
                    ID = context.Request.Params["hdID"];
                    var o_model = bll.GetModel(int.Parse(ID));
                    //new_model = new OnLineExam.Model.JudgeProblem();
                    o_model.ID = int.Parse(ID);
                    o_model.DepID = o_model.DepID;
                    //o_model.CourseID = int.Parse(context.Request.Params["selectCourse"]);
                    o_model.PaperID =  int.Parse(context.Request.Params["selectPaper"]);
                    o_model.FrontTitle = context.Request.Params["txtTitle"];
                    
             

                    o_model.Answer = context.Request.Params["txtAnswer"];

                    if (bll.Update(o_model))
                    {
                        context.Response.Write(ID);//修改成功，返回ID
                    }
                    else
                    {
                        context.Response.Write("0");//修改失败，返回0
                    }
                    #endregion
                    break;
                case "del":
                    #region 删除
                    ID = context.Request.Params["ID"];
                    if (bll.Delete(int.Parse(ID)))
                    {
                        context.Response.Write(ID);//删除成功，返回ID
                    }
                    else
                    {
                        context.Response.Write("0");//删除失败，返回0
                    }
                    #endregion
                    break;
                default:
                    break;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}